// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    
    private dt_count_temp:number = 1/40;   //40帧   
    private dt_count:number = 0;           //计时器    
    private gravite_speed = 300;          //物体的重力速度
    private set_gravite_speed = 300;       //设置物体的重力速度
    private game_flag:boolean = true;      //游戏开始标志
    private cur_pos_y = 0;                 //碰撞前的坐标
    private move_status = true;            //运动标志
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.gravite_speed = this.set_gravite_speed;
    }

    start () {

    }

    update_function(dt){
        if(this.game_flag == true)
        {           
            var s = dt * this.gravite_speed;
            if(this.gravite_speed == 0)
            {
                Math.round(this.node.y/100)*100;
            }
            else
            {
                this.node.y -= s;
            }
            
        }       
    }

    update (dt) {        
        this.dt_count = this.dt_count + dt;
        if(this.dt_count >= this.dt_count_temp)
        {
            this.update_function(dt);
            this.dt_count = 0;
        }
    }

    onCollisionStay(other, self) {
        //console.log('on collision stay',other,self); 
       
             
    }

    onCollisionEnter(other,self) {
        console.log('head onCollisionEnter:',other,self);
        console.log('self.tag',self.tag);
        if(self.tag == 2)
        {
            if(other.name == 'ground<BoxCollider>')
            {
                this.stop_gravite_move(); 
            }
        }
    }

    onCollisionExit(other,self) {
        console.log('on collision exit');
        if(self.tag == 2)
        {
            if(other.name == 'ground<BoxCollider>')
            {
                this.start_gravite_move();  
            }
        }
    }

    start_gravite_move(){
        this.gravite_speed = this.set_gravite_speed;
    }

    stop_gravite_move(){
        this.gravite_speed = 0;        
    }
}
